/**
Application: upload file
Author:chilei 
Date:2012/12/3
*/
Ext.namespace("Ext.clNameSpace");

Ext.clNameSpace.uploadWin = function(config){
	var uploadFormPanel = new Ext.form.FormPanel({
		id:'uploadForm',
		baseCls: 'x-plain',
		labelAlign:'right',
		labelWidth:60,
		frame:true,
		fileUpload:true,
		defaults:{
			allowBlank:false
		},
		items:[
		{id:'uploadfile',name:'file',fieldLabel:'选择上传的文件,大小不能超过100M',xtype:'textfield',inputType:'file',blankText:'不能为空',emptyText:'必填',anchor:'98%',allowBlank:false},
		{id:'filename',name:'uploadTitle',fieldLabel:'文件标题',xtype:'textarea',blankText:'不能为空',emptyText:'必填',anchor:'98%'},
		{id:'keywords',name:'uploadKeywords',fieldLabel:'关键词,多个关键词用,隔开,建议不超过10个词',xtype:'textarea',blankText:'不能为空',emptyText:'必填',anchor:'98%',maxLength:300},
		{id:'describe',name:'uploadDescribe',fieldLabel:'文件描述',xtype:'textarea',blankText:'不能为空',emptyText:'必填',anchor:'98%'}
		]
	});
	
	
	
	var _config = Ext.apply({
		id:'uploadwin',
		title:'上传文件',
		width:600,
		height:400,
		closable:true,
		collapsible:false,
		resizable:false,
		defaults:{
			border:false
		},
		buttonAlign:'center',
		buttons:[
		{text:'上传',type:'submit',id:'sb',height:100,handler:upload},
		{text:'取消',height:100,handler:function(){
			Ext.getCmp('uploadwin').close();
		}}],
		keys:[
		{key:Ext.EventObject.ENTER,fn:upload,scope:this}
		],
		items:[
			uploadFormPanel
		]
			
	},config);
	Ext.clNameSpace.uploadWin.superclass.constructor.call(this,_config);
};
Ext.extend(Ext.clNameSpace.uploadWin,Ext.Window,{});

Ext.clNameSpace.uploadnewWin = function(config){
	var uploadFormPanel = new Ext.form.FormPanel({
		id:'uploadForm',
		baseCls: 'x-plain',
		labelAlign:'right',
		labelWidth:60,
		frame:true,
		fileUpload:true,
		defaults:{
			allowBlank:false
		},
		items:[
		{id:'uploadfile',name:'file',fieldLabel:'选择上传的文件,大小不能超过100M',xtype:'textfield',inputType:'file',blankText:'不能为空',emptyText:'必填',anchor:'98%',allowBlank:false},
		{id:'verdescri',name:'uploadVersionDescribe',fieldLabel:'描述新版本',xtype:'textfield',blankText:'不能为空',emptyText:'必填',anchor:'98%'},
		{id:'filename',name:'uploadTitle',fieldLabel:'文件标题',xtype:'textarea',blankText:'不能为空',emptyText:'必填',anchor:'98%'},
		{id:'keywords',name:'uploadKeywords',fieldLabel:'关键词,多个关键词用,隔开,建议不超过10个词',xtype:'textarea',blankText:'不能为空',emptyText:'必填',anchor:'98%',maxLength:300},
		{id:'describe',name:'uploadDescribe',fieldLabel:'文件描述',xtype:'textarea',blankText:'不能为空',emptyText:'必填',anchor:'98%'}
		]
	});
	
	
	
	var _config = Ext.apply({
		id:'uploadwin',
		title:'上传新版本',
		width:600,
		height:400,
		closable:true,
		collapsible:false,
		resizable:false,
		defaults:{
			border:false
		},
		buttonAlign:'center',
		buttons:[
		{text:'上传新版本',type:'submit',id:'sb',height:100,handler:uploadnewversion},
		{text:'取消',height:100,handler:function(){
			Ext.getCmp('uploadwin').close();
		}}],
		keys:[
		{key:Ext.EventObject.ENTER,fn:upload,scope:this}
		],
		items:[
			uploadFormPanel
		]
			
	},config);
	Ext.clNameSpace.uploadnewWin.superclass.constructor.call(this,_config);
};
Ext.extend(Ext.clNameSpace.uploadnewWin,Ext.Window,{});

function upload(){
	var uploadFormPanel = Ext.getCmp("uploadForm");
	var uploadfile = Ext.getCmp("uploadfile");
	var filefilter = /#|&|@|\+/
	
	if(filefilter.test(uploadfile.getValue().indexOf("#"))){
		Ext.Msg.alert("文件名不能有#");
		return;
	}
		if(uploadFormPanel.form.isValid()){
			uploadFormPanel.form.submit({
				waitMsg:'正在上传',
				url:'login/uploadfile.action',
				success: function (form, action) {  
					
					if(action.result.msg == 'OK'){
          				Ext.Msg.alert("上传成功!");  
          				Ext.getCmp("uploadwin").close();
          				Ext.getCmp("filepage_panel").store.reload();
          			}
          			else if(action.result.msg == 'FAIL'){
          				Ext.Msg.alert("上传失败,文件存在!");  
          				Ext.getCmp("uploadwin").close();
          			}else if(action.result.msg == 'SIZE'){
          				Ext.Msg.alert("文件太大");
          			}
       	 		},  
                  
        		error: function (data, status, e) {  
           			alert(e);  
        		} 
			});
		}		 
}
function uploadnewversion(){
	var grid = Ext.getCmp("filepage_panel");
	var filelist = new Array();
    if(grid.getSelectionModel().hasSelection()){
    	var records = grid.getSelectionModel().getSelections();    	
    	for(var i = 0; i < records.length; i=i+1){
    		filelist[i] = records[i].data.title;	
    	}
    }else{
    	Ext.Msg.alert("请选择");
    	return;
    }
    if(filelist.length > 1){
    	Ext.Msg.alert("请选择一个");
    	return;
    }
    
	var uploadFormPanel = Ext.getCmp("uploadForm");
	var uploadfile = Ext.getCmp("uploadfile");
	var filefilter = /#|&|@|\+/
	if(filefilter.test(uploadfile.getValue().indexOf("#"))){
		Ext.Msg.alert("文件名不能有#");
		return;
	}
		if(uploadFormPanel.form.isValid()){
			uploadFormPanel.form.submit({
				waitMsg:'正在上传',
				url:'login/updatefile.action?oldname='+encodeURI(encodeURI(filelist[0])),
				dataType:'content',
				success: function (form, action) {  
          			if(action.result.msg == 'OK'){
          				Ext.Msg.alert("上传成功!");  
          				Ext.getCmp("uploadwin").close();
          				Ext.getCmp("filepage_panel").store.reload();
          			}
          			else if(action.result.msg == 'FAIL'){
          				Ext.Msg.alert("上传失败!");  
          				Ext.getCmp("uploadwin").close();
          			}else if(action.result.msg == 'NAME'){
          				Ext.Msg.alert("文件名不对");  
          				Ext.getCmp("uploadwin").close();
          			}
       	 		},  
                  
        		error: function (data, status, e) {  
           			alert(e);  
        		} 
			});
		}
}